package dao.impl;


import bean.Athlete;
import dao.AthleteDao;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Auther leiHua
 * @Date 2022/5/13 21:21
 */
public class AthleteDaoImpl implements AthleteDao {
    /**
     * 根据id查询运动员信息
     *
     * @param id 运动员id
     * @return 一个运动员信息类，失败则返回空
     */

    public Athlete select(String id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("select * from athlete where a_id=?");
            ps.setString(1, id);
            rs = ps.executeQuery();
            Athlete athlete = new Athlete();
            if (rs.next()) {
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String dept = rs.getString("department");
                athlete.setA_id(id);
                athlete.setName(name);
                athlete.setSex(sex);
                athlete.setAge(age);
                athlete.setDepartment(dept);
                return athlete;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return null;
    }

    /**
     * 查询所有的运动员信息
     * @return  运动员类列表
     */
    public ArrayList<Athlete> selectAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("select * from athlete ");
            rs = ps.executeQuery();
            ArrayList<Athlete> athleteList = new ArrayList<>();
            while (rs.next()) {
                Athlete athlete = new Athlete();
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String dept = rs.getString("department");
                String id = rs.getString("a_id");
                athlete.setA_id(id);
                athlete.setName(name);
                athlete.setSex(sex);
                athlete.setAge(age);
                athlete.setDepartment(dept);
                athleteList.add(athlete);
            }
            return athleteList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return null;
    }

    public Athlete selectDependName(String name, Object o) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("select * from athlete where name=?");
            ps.setString(1, name);
            rs = ps.executeQuery();
            Athlete athlete = new Athlete();
            if (rs.next()) {
                String a_id = rs.getString("a_id");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String dept = rs.getString("department");
                athlete.setA_id(a_id);
                athlete.setName(name);
                athlete.setSex(sex);
                athlete.setAge(age);
                athlete.setDepartment(dept);
                return athlete;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return null;
    }

    public boolean update(Athlete athlete) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("update athlete set name=?,sex=?,age=?,department=?,c_id=? where a_id=?");
            ps.setString(1, athlete.getName());
            ps.setString(2, athlete.getSex());
            ps.setInt(3, athlete.getAge());
            ps.setString(4, athlete.getDepartment());
            ps.setString(5, athlete.getC_id());
            ps.setString(6, athlete.getA_id());

            int i = ps.executeUpdate();
            if (i == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return false;
    }

    public static void main(String[] args) {
        AthleteDaoImpl athleteDao = new AthleteDaoImpl();
        System.out.println(athleteDao.select("1"));

    }

    public boolean add(Athlete athlete) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("insert into athlete values (?,?,?,?,?,?)");
            ps.setString(1, athlete.getA_id());
            ps.setString(2, athlete.getName());
            ps.setString(3, athlete.getSex());
            ps.setInt(4, athlete.getAge());
            ps.setString(5, athlete.getDepartment());
            ps.setString(6, athlete.getC_id());
            int i = ps.executeUpdate();
            if (i == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return false;
    }

    public boolean delete(String id) {
        try {
            Connection conn = JdbcUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement("delete from athlete where a_id=?");
            ps.setString(1, id);
            int i = ps.executeUpdate();
            if(i==1) return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

        }
        return false;
    }
}

